from django.contrib import admin
from django.urls import path
from rest_framework.routers import SimpleRouter
from .views import UserView,index,InsertDataToRedis

router = SimpleRouter()

router.register('study', UserView, 'study')
urlpatterns = [
    # path('redis_test/', MyRedisView.as_view()),
    # path('celery_test/', CeleryView.as_view()),
    # path('sckill/', SckillView.as_view()),
    path('test/', index),
    path('insert_data/', InsertDataToRedis.as_view(), name='insert_data_to_redis'),

]

urlpatterns += router.urls


# def validate(self, attrs):
#     print(attrs)
#     username = attrs.get('username')
#     password = attrs.get('password')
#     print(username, password)
#     # user = authenticate(username=username, password=password)
#     try:
#         user = User.objects.all().get(username=username)
#     except Exception as e:
#         raise APIException('user not exist')
#     print(check_password(password, user.password))
#     if not check_password(password, user.password):
#         # if user and not check_password(password,user.password):
#         if self.number[0] > 0:
#             print('111>>>>>>>>>>>>>>>>')
#             output = f'用户名或者密码错误，你还有{self.number[0]}次机会'
#             print(self.number[0])
#             self.number[0] -= 1
#             raise APIException(output)
#         else:
#             print('222222222222>>>>>>>>>>>>>>')
#             User.objects.all().filter(username=username).update(is_active=0)
#             raise APIException('你已经输错三次密码，请等待管理员激活！')
#     user_active = User.objects.all().filter(username=username, is_active=1).first()
#     # gender = user.doctor.gender
#     # mobile = user.doctor.mobile
#     # print(gender)
#     if user_active:
#         token = self._get_token(user)
#         # 在userToken表中存储一下：1 从来没有登录过，插入一条，     2 登录过，修改记录
#         # 如果有就修改，如果没有就新增  (if 自己写)
#         # kwargs 传入的东西查找，能找到，使用defaults的更新，否则新增一条
#         UserToken.objects.update_or_create(user=user, defaults={'token': token})
#         self.context['id'] = user.id
#         self.context['username'] = user.username
#         user_info = {'name': user.name,
#                      'phone': user.phone,
#                      'team_number': user.team.number}
#         # 'user_type': user.user_type,
#         # 'icon': 'http://127.0.0.1:8000/media/' + str(user.icon)}
#         self.context['user_info'] = user_info
#         # self.context['gender'] = gender
#         # self.context['mobile'] = mobile
#         # self.context['user_type'] = user.get_user_type_display()
#         self.context['token'] = token
#         # self.context['icon'] = 'http://127.0.0.1:8000/media/' + str(user.icon)
#         return attrs
#     else:
#         raise APIException('等待激活')